﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Script.UI;
using UnityEngine.UI;
using XCharts;
using Script.UI.Component;
using LitJson;
using MTUnity.Utils;
using System;
using UnityEngine.SocialPlatforms;

public class UI_Energy : UIUpdateBase
{
    public HistogramGroup Panel_6H;
    public HistogramGroup Panel_12H;

    public LiquidChart Liquid_Energy;
    public Text Txt_Energy;
    public Text Txt_RefreshTime;

    #region Interface
    public override Dictionary<string, UpdateDel> RegisterProcessorMethod()
    {
        JsonData json = new JsonData();

        return new Dictionary<string, UpdateDel>
        {
            {JsonFildName.SortingLine.Current_EnergyConsumption , UpdateCurrentEnergyConsumption },
            {JsonFildName.SortingLine.Record_EnergyConsumption , UpdateRecordEnergyConsumption },
            {JsonFildName.SortingLine.EnergyConsumptionRefreshTime , UpdateRefreshTime },
        };
    }
    #endregion

    #region UpdateInterface
    public void UpdateCurrentEnergyConsumption(JsonData jsonData)
    {
        if (jsonData.IsDouble)
        {
            UpdateCurrentEnergyConsumption(JsonUtils.ToDouble(jsonData));
        }
    }

    public void UpdateRecordEnergyConsumption(JsonData jsonData)
    {
        if (jsonData.IsArray)
        {
            List<HistogramDataBase> listData = Utils.SetData<HistogramData_EnergyConsumption>(jsonData, JsonFildName.SortingLine.EnergyConsumption);

            UpdateRecordEnergyConsumption(listData);
        }
    }

    public void UpdateRefreshTime(JsonData jsonData)
    {
        UpdateRefreshTime(JsonUtils.ToLong(jsonData));
    }
    #endregion

    private void UpdateCurrentEnergyConsumption(double dCurrentEnergyConsumption)
    {
        Txt_Energy.text = ((int)dCurrentEnergyConsumption).ToString() ;
        Liquid_Energy.UpdateData(0, 0, (float)dCurrentEnergyConsumption);
    }

    private void UpdateRecordEnergyConsumption(List<HistogramDataBase> listData)
    {
        Panel_6H.Refresh(listData);
        Panel_12H.Refresh(listData);
    }

    public void UpdateRefreshTime(long lRefreshTime)
    {
        DateTime datatime = TimeUtil.FromTimestamp(lRefreshTime);
        Txt_RefreshTime.text = string.Format("数据更新 {0}", datatime.ToString("{yyyy-MM-dd HH:mm:ss}"));
    }
}